/* 
 *  Copyright (c) 2011—2013 Panguso.com. All rights reserved. 
 *  File Name:  AccountHelper.java                                      
 *  File Contents Outline :  用户持久化类
 *  File  Create Date:   2012年7月30日
 *  File Version#: Revision#: #1                  
 *  File  Latest Modify Date : 2013年1月16日
 *  File Author :  bachuan@panguso.com
 */

package storage.dbhelper;

import cn.edu.thu.log.web.service.PropertiesReaderServices;
import cn.edu.thu.log.web.service.impl.PropertiesReaderServicesImpl;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.cassandra.thrift.ColumnOrSuperColumn;
import org.apache.cassandra.thrift.KeySlice;
import storage.dbobject.DbAccount;
import storage.util.StorageManager;


public class AccountHelper {
    /**
     * 
     * @param account 
     */
    public void saveAccount(DbAccount account){
            PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
        StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"), x1.getProperties("db_columnFamily_account"));
    //    StorageManager sm = new StorageManager(StorageParameter.db_keySpace, StorageParameter.db_columnFamily_account);
//        sm.open();
        sm.setRowKey(account.getRowKey());
        sm.put(DbAccount.cl_username, account.getUsername());
        sm.put(DbAccount.cl_password, account.getPassword());
        sm.put(DbAccount.cl_group, account.getGroup());
//        sm.close();
    }
    
    public List<DbAccount> getAccounts(final String columnName, final String value) {
        List<DbAccount> result = new LinkedList<DbAccount>();
       PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
        StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"), x1.getProperties("db_columnFamily_account"));
//        sm.open();
        List<KeySlice> crs = sm.get(columnName, value);
        try {
            for (KeySlice ks : crs) {
                String rowKey = new String(ks.getKey());
                String username=null,password=null,group=null;
                for (ColumnOrSuperColumn column : ks.getColumns()) {
                    String columnsName = new String(column.getColumn().getName());
                    String columnsValue = new String(column.getColumn().getValue());
                    if (columnsName.equals(DbAccount.cl_username)) {
                        username = columnsValue;
                    } else if (columnsName.equals(DbAccount.cl_password)) {
                        password = columnsValue;
                    } else if (columnsName.equals(DbAccount.cl_group)) {
                        group = columnsValue;
                    } 
                }
                DbAccount account = new DbAccount(rowKey, username, password, group);
                result.add(account);
            }
        } catch (Exception ex) {
            Logger.getLogger(AccountHelper.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
//            sm.close();
        }
        return result;
    }
    
    public void deleteAccount(final String rowKey){
     PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
        StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"), x1.getProperties("db_columnFamily_account"));
//        sm.open();
        sm.delete(rowKey);
//        sm.close();
    }
}
